/**
 * 
 */
package com.canadroid;

import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.opengl.texture.region.TextureRegion;


/**
 * @author aquattrini
 *
 */

public class Guy extends Sprite 
{
	float timer;
	private float currentVelocY;
	private float initialVelocY;
	private float initialY;
	private float gravity;
	boolean isJumping;
	boolean up;
	float posY_Orig;
	float tUp;
	

	public Guy(float pX, float pY, TextureRegion pTextureRegion) 
	{
		super(pX, pY, pTextureRegion);
		currentVelocY = 0.1f;
		timer = 0;
		gravity = 19.0f;
		isJumping = false;
		up = true;
		posY_Orig = this.getY();
	}
	
	public void Jump(float speed)
	{
		if(!isJumping)
		{
			timer = 0;
			gravity = 19.0f;
			initialVelocY = speed;
			isJumping = true;
			initialY = this.getY();
		}
	}
	
	
	@Override
    public void onManagedUpdate(final float dt) 
	{
		float posY;
		
		if(isJumping)
		{
			timer += dt;
			
			currentVelocY = initialVelocY - gravity * timer;
			
			tUp = initialVelocY / gravity;
			
			if(timer >= tUp)
			{
				//gravity = -19;
			}	

			posY = initialY + initialVelocY * timer - 0.5f * (gravity * timer * timer);

			this.setPosition(this.getX(), posY);
			
			if (posY <= posY_Orig)
				isJumping = false;
		}
    }

}
